/**
 * @description 订单JS
 * @author 赵子靖
 * @since 2018-07-08
 */
var Customer = function() {
    var t = {
        path :'',
        init : function() {
            t.initView();
            t.initEvent();
        },
        initView : function() {
            
        },
        initEvent : function() {
            
        },
        render:function(){
            $("#customer-table").bootstrapTable('destroy');
            $("#customer-table").bootstrapTable({
                queryParams : function(params) {
                    $.extend(params, {
                    })
                    return params;
                }
            });
            $("#customer-table").bootstrapTable('refresh');
        },
        operator : function(value, row) {
            var html = "";
            html += '<a href="javascript:Customer.updateCustomerDialog(\'' + value + '\')" class="btn btn-default btn-xs">编辑</a>&nbsp;';
            html += '<a href="javascript:Customer.deleteCustomerDialog(\'' + value + '\')" class="btn btn-default btn-xs">删除</a>';
            return html;
        },
		getTime : function(value, row){
			var date = new Date(value);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	        Y = date.getFullYear() + '-';
	        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	        D = date.getDate() + ' ';
	        h = date.getHours() + ':';
	        m = date.getMinutes() + ':';
	        s = date.getSeconds();
	        return Y+M+D+h+m+s;
		 },
        
        addCustomerDialog : function(){
            $("#dialog").load(t.path+"/customer/forwardAddCustomer.do",function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#add-customer-dialog').modal();
                }else if(statusTxt=="error"){
                    layer.alert("添加失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        
        // 添加客户+修改客户
        addCustomer : function(){
        	var cNo = $("#cNo").val();
        	var cName = $("#cName").val();
            var cContact = $("#cContact").val();
            var cMobile = $("#cMobile").val();
            var cAddress = $("#cAddress").val();
            if(!(/^1[3|4|5|7|8]\d{9}$/.test(cMobile))){
            	alert("手机号码输入有误，请重新输入！");
            	return false;
            }
            // a==1时为添加客户，a==2时为修改客户
//          var urlURL = null; 
//          if(a==2){
//            	urlURL = t.path+"/customer/updateCustomer.do";
//            }else if(a==1){
//            	urlURL = t.path+"/customer/addCustomer.do";
//            }           
            $.ajaxFileUpload({
                url:t.path+"/customer/addCustomer.do",
                secureuri:false,
//              fileElementId:["aHeadImg"],
                dataType:"json",
                cache:false,
                data:{
                	cNo:cNo,
                    cName:cName,
                    cContact:cContact,
                    cMobile:cMobile,
                    cAddress:cAddress  
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("保存成功!");
                        $("#add-customer-dialog").modal('toggle');
                        $("#customer-table").bootstrapTable('refresh');
                    }else{
                        alert("保存失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert(e);   
                }
            });
        },
        //编辑按钮跳转
        updateCustomerDialog:function(id){
            $("#dialog").load(t.path+"/customer/forwardUpdateCustomerPage.do?id="+id,function(responseTxt,statusTxt,xhr){
                if(statusTxt=="success"){
                    $('#update-customer-dialog').modal();
                }else if(statusTxt=="error"){
                    layer.alert("编辑失败", {
                        closeBtn: 0,
                        shift: 2 //动画类型
                    });
                }
            });
        },
        //修改保存
        updateCustomer : function(cId){
        	var cNo = $("#cNo").val();
        	var cName = $("#cName").val();
            var cContact = $("#cContact").val();
            var cMobile = $("#cMobile").val();
            var cAddress = $("#cAddress").val();
            if(!(/^1[3|4|5|7|8]\d{9}$/.test(cMobile))){
            	alert("手机号码输入有误，请重新输入！");
            	return false;
            }          
            $.ajaxFileUpload({
                url:t.path+"/customer/updateCustomer.do?cId="+cId,
                secureuri:false,
                dataType:"json",
                cache:false,
                data:{
                	cNo:cNo,
                    cName:cName,
                    cContact:cContact,
                    cMobile:cMobile,
                    cAddress:cAddress  
                },
                success:function(data,status){//服务器响应成功处理
                    if(data.success){
                        alert("修改成功!");
                        $("#update-customer-dialog").modal('toggle');
                        $("#customer-table").bootstrapTable('refresh');
                    }else{
                        alert("修改失败!");
                    }
                },
                error:function(data,status,e){//服务器响应失败处理
                    alert("系统错误码："+status);   
                }
            });
        },
        //删除
        deleteCustomerDialog:function(cId){
                bootbox.confirm({
                size : 'small',
                message : "确认删除该信息，删除后将不可恢复？",
                callback : function(result) {
                    if (result) {
                        $.ajax({
                            type:"POST",
                            url:t.path+"/customer/delCustomer.do?cId="+cId,
                            dataType:"json",
                            data:{
                                
                            },
                            async: false,
                            cache:false,
                            success:function(data){
                                if (data && data.success) {
                                    alert("删除成功");
                                    $("#customer-table").bootstrapTable('refresh');
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                    }
                }
            });
        }
    }
    return t; 
}();